﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Common
{
   public class SortHelper
    {
        /// <summary>
        /// 排序,方式不为DESC或ASC时不排序
        /// </summary>
        /// <param name="sortProperty">排序的字段</param>
        /// <param name="sortDirection">排序的方式（ASC,DESC）</param>
        /// <param name="list">要排序的List</param>
       public static void SortList<E>(string sortProperty, string sortDirection, List<E> list)
       {
           switch (sortDirection)
           {
               case "ASC":
                   list.Sort(
                            new Comparison<E>
                             (
                              delegate(E x, E y)
                              {

                                  return String.Compare(
                                         (x.GetType()).GetProperty(sortProperty).GetValue(x, null).ToString(),
                                         (y.GetType()).GetProperty(sortProperty).GetValue(y, null).ToString()
                                                       );
                              }
                              )
                             );
                   break;
               case "DESC":
                   list.Sort(
                             new Comparison<E>
                             (
                              delegate(E x, E y)
                              {
                                  return String.Compare(
                                             (y.GetType()).GetProperty(sortProperty).GetValue(y, null).ToString(),
                                             (x.GetType()).GetProperty(sortProperty).GetValue(x, null).ToString()
                                                        );

                              }
                              )
                             );
                   break;

               default:
                   break;
           }
       }

       /// <summary>
       /// 排序,方式不为DESC或ASC时不排序
       /// </summary>
       /// 
       /// <param name="sortDirection">排序的方式（ASC,DESC）</param>
       /// <param name="list">要排序的List</param>
       public static void SortList(string sortDirection, List<string> list)
       {
           switch (sortDirection)
           {
               case "ASC":
                   list.Sort(new Comparison<string>
                               (
                                 delegate(string x, string y)
                                 {
                                   return String.Compare(x,y);
                                 }
                               )
                            );
                   break;
               case "DESC":
                   list.Sort(new Comparison<string>
                              (
                                delegate(string x, string y)
                                 {
                                   return String.Compare(y,x);
                                 }
                              )
                            );
                   break;

               default:
                   break;
           }
       }
    }
}
